function unescapeHTML(text) {
    var div = document.createElement('div');
    div.innerHTML = text;
    return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
}

var web_data;
var news_data;
var flickr_data
var amzn_data;

function clearAll(){
    var d=["web","news","flickr","amzn"];
    for(var i in d){
        document.getElementById(d[i]).style.display="none";
        for(var j=0;j<4;j++){
            var e=document.getElementById(d[i]+"_"+j);
            if(e)e.innerHTML="";
        }
    }
    myEditor.clearEditorDoc();
}
function updateSmartStuff(content){
    if(content){
        content=content.replace(/<[^<>]*>/g,'').replace(/^s+/, '').replace(/s+$/, '');
        if(content){
            content=unescapeHTML(content);
        }else{
            return
        }
    }else{
        return;
    }

    web_data=new Object;
    news_data=new Object;
    flickr_data=new Object;
    amzn_data=new Object;

//    clearAll();
    if(content && content.length>2048)
        content = content.substring(content.length-2048,content.length);
    get_kwds(content, "process_contextual_kwds");
}
///////////////////Analysis callbacks////////////////////////////
function process_contextual_kwds(data){
    var kwds=new Array;
    if(data && data.ResultSet){
        kwds=data.ResultSet.Result;
    }
    var str='';

    for(var i=0; i<3&&i<kwds.length;i++){
//        if(i>=3)break;
        get_web_search(kwds[i],"web_callback_"+i);
        get_news_search(kwds[i],"news_callback_"+i);
        get_flickr_search(kwds[i],"flickr_callback_"+i);
        get_amzn_search(kwds[i],"amzn_callback_"+i);
    }
    for(var j=i;j<3;j++){
        web_callback(j,null);
        news_callback(j,null);
        flickr_callback(j,null);
        amzn_callback(j,null);
    }
    if(kwds.length>=3){
        get_flickr_search(kwds[3],"flickr_callback_3");
    }else{
        flickr_callback(3,null);
    }
}
///////////////////Web search callbacks/////////////////////////
function web_callback_0(data){
    web_callback(0,data)
}

function web_callback_1(data){
    web_callback(1,data)
}
function web_callback_2(data){
    web_callback(2,data)
}

function web_callback(index, data){
    document.getElementById("web_" + index).innerHTML = ""
    if (data && data.ResultSet.Result.length > 0) {
//        if (!web_data[data.ResultSet.Result[0].Title]) {
            document.getElementById("web").style.display = "block";
            document.getElementById("web_" + index).innerHTML = "<a target=_new href='" + data.ResultSet.Result[0].ClickUrl + "' style='text-decoration:none'><b>" + data.ResultSet.Result[0].Title.substr(0, 60) + "</b><br><span style='font-size:11px'>" + data.ResultSet.Result[0].Summary.substring(0, 75) + "</span></a>";
//            web_data[data.ResultSet.Result[0].Title] = true;
//        }
    }
}

///////////////////////News Callback////////////////////////////
function news_callback_0(data){
    news_callback(0,data)
}

function news_callback_1(data){
    news_callback(1,data)
}
function news_callback_2(data){
    news_callback(2,data)
}

function news_callback(index, data){
    document.getElementById("news_"+index).innerHTML=""
    if(data && data.ResultSet.Result.length>0){
//        if(!news_data[data.ResultSet.Result[0].Title]){
            document.getElementById("news").style.display="block";
            document.getElementById("news_"+index).innerHTML="<a target=_new href='"+data.ResultSet.Result[0].ClickUrl+"' style='text-decoration:none'><b>"+data.ResultSet.Result[0].Title.substr(0,60)+"</b><br><span style='font-size:11px'>"+data.ResultSet.Result[0].Summary.substring(0,75)+"</span></a>";
//            news_data[data.ResultSet.Result[0].Title]=true;
//        }
    }
}

///////////////////////Flickr callback////////////////////////////
function flickr_callback_0(data){
    flickr_callback(0,data)
}

function flickr_callback_1(data){
    flickr_callback(1,data)
}
function flickr_callback_2(data){
    flickr_callback(2,data)
}
function flickr_callback_3(data){
    flickr_callback(3,data)
}

function flickr_callback(index, data){
    document.getElementById("flickr_"+index).innerHTML=""
    if(data&&data.items.length>0){
//        if(!flickr_data[data.items[0].link]){
            document.getElementById("flickr").style.display="block";
            document.getElementById("flickr_"+index).innerHTML="<a target=_new href='"+data.items[0].link+"' style='text-decoration:none'><b>"+data.items[0].title.substr(0,40)+"</b><br><img src='"+data.items[0].media.m.replace(/_m.jpg/,"_s.jpg")+"' border=0></a>";
//            flickr_data[data.items[0].link]=true;
//        }
    }
}

///////////////////////Amzn callback////////////////////////////
function amzn_callback_0(data){
    amzn_callback(0,data)
}

function amzn_callback_1(data){
    amzn_callback(1,data)
}
function amzn_callback_2(data){
    amzn_callback(2,data)
}

function amzn_callback(index, data){
    document.getElementById("amzn_"+index).innerHTML=""
    if(data&&data.ItemSet.Item.length>1){
//        if(!amzn_data[data.ItemSet.Item[0].title]){
            document.getElementById("amzn").style.display="block";
            document.getElementById("amzn_"+index).innerHTML="<a target=_new href='"+data.ItemSet.Item[0].url+"' style='text-decoration:none'><b>"+data.ItemSet.Item[0].title+"</b><br><span style='font-size:12px' >"+data.ItemSet.Item[0].price+"</span></a>";
//            amzn_data[data.ItemSet.Item[0].title]=true;
//        }
    }
}

//////////////////////////////////////////////////////////////////

function get_amzn_search(kwd, callback){
    var url="http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService&SubscriptionId=19267494ZR5A8E2CGPR2&AssociateTag=hackdayindia-20&Operation=ItemSearch&Style=http://kokogiak.com/amazon/JSON/ajsonCategorySearch.xsl&ContentType=text/javascript&ItemPage=1&SearchIndex=All&ResponseGroup=Small,Offers&MerchantId=ATVPDKIKX0DER&Availability=Available&Condition=All&sort=relevanceRank&CallBack="+callback+"&Keywords="+escape(kwd);
    web_call_basic(url,callback);
}


function get_flickr_search(kwd, callback){
    var url="http://api.flickr.com/services/feeds/photos_public.gne?lang=en-us&format=json&jsoncallback="+callback+"&tags="+escape(kwd);
    web_call_basic(url,callback);
}

function get_news_search(kwd, callback){
    var url="http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&results=1&language=en&query="+escape(kwd);
    web_call(url,callback);
}

function get_web_search(kwd, callback){
    var url="http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&result=1&query="+escape(kwd);
    web_call(url, callback);
}

function get_kwds(content, callback){
    var url="http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction?appid=YahooDemo&context="+escape(content).replace(/^%A0/,'');
    web_call(url, callback);
}

function web_call(url, callback){
    url=url+"&output=json&callback="+callback;
    web_call_basic(url,callback);
}

function web_call_basic(url, callback){
    var sc=document.createElement("script");
    sc.src=url;
    document.body.appendChild(sc);
}

//